package com.cm.log_process.log_query.controller;

import com.cm.log_process.log_common.config.ConfigBean;
import com.cm.log_process.log_common.entity.*;
import com.cm.log_process.log_common.entity.RequestParam;
import com.cm.log_process.log_common.exp.IllegalPhoneException;
import com.cm.log_process.log_common.exp.NoIndexException;
import com.cm.log_process.log_common.exp.ParamNotMatchException;
import com.cm.log_process.log_common.reg.BusinessRegexp;
import com.cm.log_process.log_common.annotation.OperateLog;
import com.cm.log_process.log_query.service.MultiQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Creator:郭兆鹏
 * Date:2019/12/16
 * 思路：综合查询异常
 */
@Slf4j
@RestController
@RequestMapping("multiquery")
public class MultiQueryController {

    private static BusinessRegexp reg = new BusinessRegexp();


    @Autowired
    private MultiQueryService multiQueryService;

    /**
     * 根据流水单号和时间戳分页查询
     * @param param
     * @return
     * @author 沈万龙
     */
    @OperateLog(oType = "_search",oName = "通过流水单号查询日志（带时间戳）")
    @RequestMapping(value = "/byTransId", method = RequestMethod.POST)
    public Result<Log> byTransId(@RequestBody RequestParam param) {
        PageResult<Log> logPageResult;
        //判断是索引是否为空(pageIndex和pageSize都有默认值，可以不用判断)
        if (StringUtils.isNotEmpty(param.getIndexName())) {
            if (StringUtils.isNotEmpty(param.getTransId())) {
                //startTime和endTime已经设置了初值，所以不用分开进行判断
                logPageResult = multiQueryService.byTransId(param);
                return new Result<Log>(StatusCode.SUCCESS.getCode(), logPageResult, StatusCode.SUCCESS.getMessage());
            }
            throw new ParamNotMatchException("缺少流水号");
        }
        throw new NoIndexException("缺少索引名称");
    }



    /**
     * @Date 2019/12/12
     * @Author 黄鹏星
     * 作用：根据HostIp查询日志
     */
    @OperateLog(oType = "_search",oName = "通过主机Ip查询日志(带时间戳)")
    @RequestMapping(value = "/byHostIp",method = RequestMethod.POST)
    public Result queryByHostIp(@RequestBody RequestParam param) {
        PageResult<Log> pageResult;
        if (StringUtils.isNotEmpty(param.getIndexName())) { //索引判空
            if (StringUtils.isNotEmpty(param.getHostIp())) {
                //动态设置index
                ConfigBean.setIndexName(param.getIndexName());
                //调用查询
                pageResult = multiQueryService.byHostIp(param);
                return new Result<>(StatusCode.SUCCESS.getCode(), pageResult, StatusCode.SUCCESS.getMessage());
            } else {
                throw new ParamNotMatchException("缺少必要参数，主机为空");
            }
        } else {
            throw new NoIndexException("缺少必要参数，索引为空");
        }
    }




    /**
     * @Date 2019/12/12
     * @Author 邓志勇
     * 作用：根据时间戳范围查询日志
     */
    @OperateLog(oType = "_search",oName = "通过时间戳查询日志(带时间戳)")
    @RequestMapping(value = "/byTimeStamp", method = RequestMethod.POST)
    public Result findMessageByTimeStamp(@RequestBody RequestParam map) {
        String index = map.getIndexName();
        if (StringUtils.isEmpty(index)) {
            throw new NoIndexException("索引名不能为空");
        }
        ConfigBean.setIndexName(index);
        PageResult<Log> pageResult = multiQueryService.findMessageByTimeStamp(map);
        return new Result<Log>(StatusCode.SUCCESS.getCode(), pageResult, "查询成功");
    }




    /**
     *@Date 2019/12/31
     *@Author 郭兆鹏
     *作用:以流水单号做主键，时间戳作为过滤条件
     */
    @OperateLog(oType = "_search",oName = "通过主机Ip查询日志(带时间戳)")
    @RequestMapping(value = "/byTransAndPhone",method = RequestMethod.POST)
    public Result<Log> byTimeStamp(@RequestBody RequestParam map){
        String indexName = map.getIndexName();
        if(StringUtils.isNotEmpty(indexName)){
            ConfigBean.setIndexName(indexName);
            String transId = map.getTransId();
            String phoneNum = map.getPhoneNum();
            if(StringUtils.isNotEmpty(transId)&&StringUtils.isNotEmpty(phoneNum)){
                if (reg.testPhoneParam(phoneNum)) {
                    PageResult<Log> logs = multiQueryService.multiSearch(map);
                    //返回查询结果
                    return new Result<>(StatusCode.SUCCESS.getCode(),logs,"查询成功");
                }else{
                    throw new IllegalPhoneException("请输入合法的手机号");
                }
            }else{
                throw new ParamNotMatchException("缺少必要参数,transId,phoneNum");
            }
        }else{
            throw new NoIndexException("缺少索引名称");
        }
    }

    //以手机号作为查询条件
    @OperateLog(oType = "_search",oName = "通过手机号查询日志(带时间戳)")
    @RequestMapping(value = "/byPhone",method = RequestMethod.POST)
    public Result<Log> queryByPhoneNumber(@RequestBody RequestParam param){
        if(StringUtils.isNotEmpty(param.getIndexName())){
            ConfigBean.setIndexName(param.getIndexName());
            if(StringUtils.isNotEmpty(param.getPhoneNum())){
                //正则匹配去除前缀后的手机号
                if(reg.testPhoneParam(param.getPhoneNum())){
                    PageResult<Log> logs = multiQueryService.byPhoneNum(param);
                    return new Result<>(StatusCode.SUCCESS.getCode(),logs,"查询成功");
                }else{
                    throw new IllegalPhoneException("请输入合法的手机号");
                }
            }else{
                throw new ParamNotMatchException("缺少主要查询条件");
            }
        }else{
            throw new NoIndexException("缺少索引名称");
        }
    }
}
